/* 
 * This file contains utility classes and functions generating textblocks for 
 * classes and interfaces.
 */

// ======================== TEXT LINE GENERATING FUNCTIONS ========================
// 
// - generateStringFromStereotypes:     generate string from an array of 
//                                      Stereotype objects.
// - generateStringFromParameters:      generate string from an array of 
//                                      Parameter objects.
// - generateTextLinesFromAttributes:   generate an array of TextLine objects from 
//                                      an array of Attribute objects.
// - generateTextLinesFromOperations:   generate an array of TextLine objects from 
//                                      an array of Operation objects.
// - generateTextLinesFromClassHeader:  generate an array of TextLine objects from 
//                                      a Class/Interface object. This array will 
//                                      represent the header of the class box

function generateStringFromStereotypes(stArr) {
    var s = "";
    if (stArr.length > 0) {
        for (var i = 0; i < stArr.length; i++) {
            if (stArr[i].name !== null && stArr[i].name !== "") {
                s += stArr[i].name;
                if (i !== stArr.length - 1) {
                    s += ", ";
                }
            }
        }
    }
    return (s !== "") ? ("<<" + s + ">>") : "";
}

function generateStringFromParameters(parArr) {
    var s = "";
    if (parArr.length > 0) {
        for (var i = 0; i < parArr.length; i++) {
            var par = parArr[i];
            if (par.stereotypes.length > 0) {
                var sstereo = generateStringFromStereotypes(par.stereotypes);
                s += (sstereo !== "") ? (sstereo + " ") : "";
            }
            s += par.name;
            if (par.type !== null) {
                s += " : " + par.type.name;
                if (par.defaultValue !== null && par.defaultValue !== "") {
                    s += " = " + par.defaultValue;
                }
            }

            if (i !== parArr.length - 1) {
                s += ", ";
            }
        }
    }
    return s;
}

function generateTextLinesFromClassHeader(model) {
    var result = new Array();
    if (model.stereotypes.length > 0) {
        var sstereo = generateStringFromStereotypes(model.stereotypes);
        if (sstereo !== "") {
            result.push(new TextLine(sstereo));
        }
    }
    var s = "";
    s += model.visibility + " " + model.name;

    var tf = new TextFormat();
    if (model.isAbstract) {
        tf.isItalic = true;
    }
    tf.isBold = true;
    var tl = new TextLine(s, tf);

    result.push(tl);

    return result;
}

function generateTextLinesFromAttributes(attributes) {
    var result = new Array();
    for (var i = 0; i < attributes.length; i++) {
        var attr = attributes[i];
        var text = "";

        text += attr.visibility + " ";
        if (attr.stereotypes.length > 0) {
            var sstereo = generateStringFromStereotypes(attr.stereotypes);
            if (sstereo !== "") {
                text += sstereo + " ";
            }
        }
        text += attr.name;
        if (attr.type !== null) {
            text += " : " + attr.type.name;
            if (attr.multiplicity !== null && attr.multiplicity !== "") {
                text += "[" + attr.multiplicity + "]";
            }
        }

        if (attr.defaultValue !== null && attr.defaultValue !== "") {
            text += " = " + attr.defaultValue;
        }

        var tf = new TextFormat();
        if (attr.isClassifierScope) {
            tf.isUnderlined = true;
        }

        var tl = new TextLine(text, tf);

        result.push(tl);
    }
    return result;
}

function generateTextLinesFromOperations(operations) {
    var result = new Array();
    for (var i = 0; i < operations.length; i++) {
        var op = operations[i];
        var text = "";

        text += op.visibility + " ";
        if (op.stereotypes.length > 0) {
            var sstereo = generateStringFromStereotypes(op.stereotypes);
            if (sstereo !== "") {
                text += sstereo + " ";
            }
        }
        text += op.name + " (";
        text += generateStringFromParameters(op.parameters);
        text += ")";
        if (op.returnType !== null) {
            text += " : " + op.returnType.name;
        }

        var tf = new TextFormat();
        if (op.isAbstract) {
            tf.isItalic = true;
        }
        if (op.isClassifierScope) {
            tf.isUnderlined = true;
        }

        var tl = new TextLine(text, tf);
        result.push(tl);
    }
    return result;
}

// ======================== RENDERER GENERATING FUNCTIONS ========================
// 
// - generateRendererForClass:  generate a renderer object from a model 
//                              (class/interface) object.
function generateRendererForClass(model) {

    var blocks = new Array();
    blocks.push(new BlockRenderer(generateTextLinesFromClassHeader(model), "center"));
    if (!model.isInterface) {
        blocks.push(new BlockRenderer(generateTextLinesFromAttributes(model.attributes), "left"));
    }
    blocks.push(new BlockRenderer(generateTextLinesFromOperations(model.operations), "left"));

    return new MultiBlockRenderer(blocks);
}
